package cn.mickey.exchange.service;

import cn.mickey.exchange.domain.model.FavoriteModel;
import cn.mickey.exchange.mapper.FavoriteMapper;
import cn.mickey.exchange.mapper.IdleItemMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 收藏的增删改查
 * user_id建索引
 */
@Service
public class FavoriteService extends ServiceImpl<FavoriteMapper, FavoriteModel> {

    @Resource
    private IdleItemMapper idleItemMapper;

    /**
     * 新增收藏
     * @param favoriteModel
     * @return
     */
    public boolean addFavorite(FavoriteModel favoriteModel) {
        return baseMapper.insert(favoriteModel) == 1;
    }

    /**
     * 删除收藏
     * @param id
     * @return
     */
    public boolean deleteFavorite(Long id) {
        return baseMapper.delete(new LambdaUpdateWrapper<FavoriteModel>()
                .eq(FavoriteModel::getFavoriteId,id)) == 1;
    }

    /**
     * 判断用户是否收藏某个闲置
     * user_id建索引
     * @param userId
     * @param idleId
     * @return
     */
    public FavoriteModel isFavorite(Long userId, Long idleId) {
         return baseMapper.selectOne(new LambdaQueryWrapper<FavoriteModel>()
                .eq(FavoriteModel::getUserId,userId).eq(FavoriteModel::getIdleId,idleId));
    }

    /**
     * 查询一个用户的所有收藏
     * @param userId
     * @return
     */
    public List<FavoriteModel> getAllFavorite(Long userId) {
        return baseMapper.getAllFavorite(new QueryWrapper<FavoriteModel>().eq("f.user_id",userId));
    }
}
